package com.changgou.goods.service.impl;

import com.changgou.entity.Result;
import com.changgou.goods.dao.SpecMapper;
import com.changgou.goods.service.SpecService;
import com.changgou.pojo.Spec;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @Author gentleman Ma
 * @Date created in 2020/2/15
 * @description
 * @Version 1.0
 */
@Service
public class SpecServiceImpl implements SpecService {

    @Autowired
    private SpecMapper specMapper;
    /**
     * 根据商品分类名称查询规格列表
     *
     * @param categoryName
     * @return
     */
    @Override
    public List<Spec> findListByCategoryName(String categoryName) {
        List<Spec> specList = specMapper.selectByCategoryName(categoryName);

        return specList;
    }

    /**
     * 查询全部数据
     *
     * @return
     */
    @Override
    public List<Spec> findAll() {
        return specMapper.selectAll();
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @Override
    public Spec findById(Integer id) {
        return specMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增数据
     *
     * @param spec
     */
    @Override
    public void add(Spec spec) {
        specMapper.insertSelective(spec);
    }

    /**
     * 根据id修改
     *
     * @param spec
     */
    @Override
    public void update(Spec spec) {
        specMapper.updateByPrimaryKeySelective(spec);
    }

    /**
     * 根据id删除
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        specMapper.deleteByPrimaryKey(id);
    }

    /**
     * 多条件搜索
     *
     * @param searchMap
     * @return
     */
    @Override
    public List<Spec> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);

        return specMapper.selectByExample(example);

    }

    /**
     * 分页查询
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Spec> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        return (Page<Spec>) specMapper.selectByExample(example);
    }


    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Spec.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null){
            //名称
            if (searchMap.get("name")!= null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }

            //规格选项
            if (searchMap.get("options") != null && !"".equals(searchMap.get("options"))){
                criteria.andLike("options","%"+searchMap.get("options")+"%");
            }

            //ID
            if (searchMap.get("id") != null){
                criteria.andEqualTo("id",searchMap.get("id"));
            }

            //排序
            if (searchMap.get("seq") != null){
                criteria.andEqualTo("seq",searchMap.get("seq"));
            }

            //模板ID
            if (searchMap.get("templateId") != null){
                criteria.andEqualTo("templateId",searchMap.get("templateId"));
            }
        }
        return example;
    }


}
